package com.example.product.service;

import com.example.grpc.product.*;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

@GrpcService
public class ProductServiceImpl extends ProductServiceGrpc.ProductServiceImplBase {

    private final ConcurrentHashMap<Long, Product> productStore = new ConcurrentHashMap<>();
    private final AtomicLong idGenerator = new AtomicLong(1);

    public ProductServiceImpl() {
        // 初始化一些示例数据
        Product product1 = Product.newBuilder()
                .setId(idGenerator.getAndIncrement())
                .setName("Laptop")
                .setDescription("High performance laptop")
                .setPrice(1200.0)
                .setQuantity(10)
                .build();
        productStore.put(product1.getId(), product1);

        Product product2 = Product.newBuilder()
                .setId(idGenerator.getAndIncrement())
                .setName("Smartphone")
                .setDescription("Latest smartphone")
                .setPrice(800.0)
                .setQuantity(25)
                .build();
        productStore.put(product2.getId(), product2);
    }

    @Override
    public void getProduct(GetProductRequest request, StreamObserver<GetProductResponse> responseObserver) {
        Product product = productStore.get(request.getId());
        if (product != null) {
            GetProductResponse response = GetProductResponse.newBuilder()
                    .setProduct(product)
                    .build();
            responseObserver.onNext(response);
        } else {
            responseObserver.onNext(GetProductResponse.newBuilder().build());
        }
        responseObserver.onCompleted();
    }

    @Override
    public void createProduct(CreateProductRequest request, StreamObserver<CreateProductResponse> responseObserver) {
        Product product = Product.newBuilder()
                .setId(idGenerator.getAndIncrement())
                .setName(request.getName())
                .setDescription(request.getDescription())
                .setPrice(request.getPrice())
                .setQuantity(request.getQuantity())
                .build();

        productStore.put(product.getId(), product);

        CreateProductResponse response = CreateProductResponse.newBuilder()
                .setProduct(product)
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

    @Override
    public void listProducts(ListProductsRequest request, StreamObserver<ListProductsResponse> responseObserver) {
        List<Product> products = new ArrayList<>(productStore.values());
        ListProductsResponse response = ListProductsResponse.newBuilder()
                .addAllProducts(products)
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}